package com.briup.corejava.day18;

import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;

/**
 * 内存流：字节数组输入流、字节数组输出流
 * 要求：读取键盘输入的字符串，保存到文件中去
 */
public class Test2 {
    public static void main(String[] args) {
        ByteArrayInputStream bais = null;
        FileOutputStream fos = null;
        // 获取键盘输入的字符串
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一些字符串：");
        String s = sc.nextLine();
        // 字符串转字节数组
        byte[] data = s.getBytes();
        bais = new ByteArrayInputStream(data);
        try {
            fos = new FileOutputStream("src/com/briup/corejava/day17/file/b.txt",true);
            byte[] b = new byte[1024];
            int len = -1;
            while((len=bais.read(b))!=-1){
                fos.write(b,0,len);
            }
            System.out.println("文件追加成功！");
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (fos!=null){
                try {
                    fos.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
